@@ -1,4 +1,6 @@ |
||
1 | 1 |
class EventsController < ApplicationController |
2 |
+ before_filter :load_event, :except => :index |
|
3 |
+ |
|
2 | 4 |
def index |
3 | 5 |
if params[:agent] |
4 | 6 |
@agent = current_user.agents.find(params[:agent]) |
@@ -14,21 +16,29 @@ class EventsController < ApplicationController |
||
14 | 16 |
end |
15 | 17 |
|
16 | 18 |
def show |
17 |
- @event = current_user.events.find(params[:id]) |
|
18 |
- |
|
19 | 19 |
respond_to do |format| |
20 | 20 |
format.html |
21 | 21 |
format.json { render json: @event } |
22 | 22 |
end |
23 | 23 |
end |
24 | 24 |
|
25 |
+ def reemit |
|
26 |
+ @event.reemit! |
|
27 |
+ redirect_to :back, :notice => "Event re-emitted" |
|
28 |
+ end |
|
29 |
+ |
|
25 | 30 |
def destroy |
26 |
- event = current_user.events.find(params[:id]) |
|
27 |
- event.destroy |
|
31 |
+ @event.destroy |
|
28 | 32 |
|
29 | 33 |
respond_to do |format| |
30 | 34 |
format.html { redirect_to events_path } |
31 | 35 |
format.json { head :no_content } |
32 | 36 |
end |
33 | 37 |
end |
38 |
+ |
|
39 |
+ private |
|
40 |
+ |
|
41 |
+ def load_event |
|
42 |
+ @event = current_user.events.find(params[:id]) |
|
43 |
+ end |
|
34 | 44 |
end |
@@ -17,4 +17,8 @@ class Event < ActiveRecord::Base |
||
17 | 17 |
def symbolize_payload |
18 | 18 |
self.payload = payload.recursively_symbolize_keys if payload.is_a?(Hash) |
19 | 19 |
end |
20 |
+ |
|
21 |
+ def reemit! |
|
22 |
+ agent.create_event :payload => payload, :lat => lat, :lng => lng |
|
23 |
+ end |
|
20 | 24 |
end |
@@ -24,6 +24,7 @@ |
||
24 | 24 |
<td> |
25 | 25 |
<div class="btn-group"> |
26 | 26 |
<%= link_to 'Show', event_path(event), class: "btn btn-mini" %> |
27 |
+ <%= link_to 'Re-emit', reemit_event_path(event), method: :post, data: { confirm: 'Are you sure you want to duplicate this event and emit the new one now?' }, class: "btn btn-mini" %> |
|
27 | 28 |
<%= link_to 'Delete', event_path(event), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-mini" %> |
28 | 29 |
</div> |
29 | 30 |
</td> |
@@ -19,7 +19,13 @@ Huginn::Application.routes.draw do |
||
19 | 19 |
end |
20 | 20 |
end |
21 | 21 |
end |
22 |
- resources :events, :only => [:index, :show, :destroy] |
|
22 |
+ |
|
23 |
+ resources :events, :only => [:index, :show, :destroy] do |
|
24 |
+ member do |
|
25 |
+ post :reemit |
|
26 |
+ end |
|
27 |
+ end |
|
28 |
+ |
|
23 | 29 |
match "/worker_status" => "worker_status#show" |
24 | 30 |
|
25 | 31 |
post "/users/:user_id/update_location/:secret" => "user_location_updates#create" |
@@ -7,7 +7,7 @@ describe EventsController do |
||
7 | 7 |
end |
8 | 8 |
|
9 | 9 |
describe "GET index" do |
10 |
- it "only returns Agents for the current user" do |
|
10 |
+ it "only returns Events created by Agents of the current user" do |
|
11 | 11 |
sign_in users(:bob) |
12 | 12 |
get :index |
13 | 13 |
assigns(:events).all? {|i| i.user.should == users(:bob) }.should be_true |
@@ -37,6 +37,28 @@ describe EventsController do |
||
37 | 37 |
end |
38 | 38 |
end |
39 | 39 |
|
40 |
+ describe "POST reemit" do |
|
41 |
+ before do |
|
42 |
+ request.env["HTTP_REFERER"] = "/events" |
|
43 |
+ sign_in users(:bob) |
|
44 |
+ end |
|
45 |
+ |
|
46 |
+ it "clones and re-emits events" do |
|
47 |
+ lambda { |
|
48 |
+ post :reemit, :id => events(:bob_website_agent_event).to_param |
|
49 |
+ }.should change { Event.count }.by(1) |
|
50 |
+ Event.last.payload.should == events(:bob_website_agent_event).payload |
|
51 |
+ Event.last.agent.should == events(:bob_website_agent_event).agent |
|
52 |
+ Event.last.created_at.should be_within(1).of(Time.now) |
|
53 |
+ end |
|
54 |
+ |
|
55 |
+ it "can only re-emit Events for the current user" do |
|
56 |
+ lambda { |
|
57 |
+ post :reemit, :id => events(:jane_website_agent_event).to_param |
|
58 |
+ }.should raise_error(ActiveRecord::RecordNotFound) |
|
59 |
+ end |
|
60 |
+ end |
|
61 |
+ |
|
40 | 62 |
describe "DELETE destroy" do |
41 | 63 |
it "only deletes events for the current user" do |
42 | 64 |
sign_in users(:bob) |
@@ -1,4 +1,19 @@ |
||
1 | 1 |
require 'spec_helper' |
2 | 2 |
|
3 | 3 |
describe Event do |
4 |
+ describe "#reemit" do |
|
5 |
+ it "creates a new event identical to itself" do |
|
6 |
+ events(:bob_website_agent_event).lat = 2 |
|
7 |
+ events(:bob_website_agent_event).lng = 3 |
|
8 |
+ events(:bob_website_agent_event).created_at = 2.weeks.ago |
|
9 |
+ lambda { |
|
10 |
+ events(:bob_website_agent_event).reemit! |
|
11 |
+ }.should change { Event.count }.by(1) |
|
12 |
+ Event.last.payload.should == events(:bob_website_agent_event).payload |
|
13 |
+ Event.last.agent.should == events(:bob_website_agent_event).agent |
|
14 |
+ Event.last.lat.should == 2 |
|
15 |
+ Event.last.lng.should == 3 |
|
16 |
+ Event.last.created_at.should be_within(1).of(Time.now) |
|
17 |
+ end |
|
18 |
+ end |
|
4 | 19 |
end |